import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout/index'

// 优化将组件懒加载提取到最开始，便于日后维护
const Login = () => import("@/views/login/index.vue") //登录
const Home = () => import("@/views/home/index.vue") // 首页
const User = () => import("@/views/user/index.vue") // 用户管理
const UserDetail = () => import("@/views/userDetail/index.vue") // 个人用户详情
const Role = () => import("@/views/role/index.vue")  // 角色管理
const Menu = () => import("@/views/menu/index.vue") // 菜单管理
const OrganChart = () => import("@/views/organChart/index.vue") // flex布局
const Log = () => import("@/views/log/index.vue") // 日志管理
const Echarts = () => import("@/views/echarts/index.vue") // echarts图表
const Cesium = () => import("@/views/cesium/index") // 铯地图
const Tencent = () => import("@/views/tencent/index") // 腾讯地图
const Openlayer = () => import("@/views/ol/index") // openlayer
const StarMap = () => import("@/views/starMap/index") // 星星地图
const Flex = () => import("@/views/flex/index.vue") // flex布局

const Cup = () => import("@/views/cup/index.vue") // 圣杯布局
const ComponentsDemo = () => import("@/views/componentsDemo/index.vue") // 组件汇总
const Center = () => import("@/views/center/index.vue") // 个人中心
const Css = () => import("@/views/css/index.vue") // css特效案例
const Test = () => import("@/views/test/index.vue") // 测试案例
const SideBar = () => import("@/views/sideBar/index.vue") // 侧边栏
const TableCompare = () => import("@/views/tableCompare/index.vue") // 表格对比

Vue.use(Router)
const routers = [
  {
    path: '/login',
    name: 'login',
    component: Login
  }, {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [{
      path: '/home',
      name: 'home',
      meta: {
        title: "首页",
        fixed: false
      },
      component: Home
    }]
  }, {
    path: '/layout',
    component: Layout,
    children: [{
      path: '/user',
      name: 'user',
      component: User,
      meta: {
        title: '用户管理'
      }
    }]
  }, {
    path: '/layout',
    component: Layout,
    children: [{
      path: '/userDetail',
      name: 'userDetail',
      component: UserDetail,
      meta: {
        title: '用户详情'
      }
    }]
  }, {
    path: '/layout',
    component: Layout,
    children: [{
      path: '/role',
      name: 'role',
      component: Role,
      meta: {
        title: '角色管理'
      }
    }]
  }, {
    path: '/layout',
    component: Layout,
    children: [{
      path: '/menu',
      name: 'menu',
      component: Menu,
      meta: {
        title: '菜单管理'
      }
    }]
  }, {
    path: '/layout',
    component: Layout,
    children: [{
      path: '/organChart',
      name: '/organChart',
      component: OrganChart,
      meta: {
        title: '组织结构'
      }
    }]
  }, {
    path: '/layout',
    component: Layout,
    children: [{
      path: '/log',
      name: 'log',
      component: Log,
      meta: {
        title: '日志管理'
      }
    }]
  }, {
    path: '/layout',
    component: Layout,
    children: [{
      path: '/echarts',
      name: 'echarts',
      component: Echarts,
      meta: {
        title: 'Echarts图表'
      }
    }]
  }, {
    path: '/layout',
    component: Layout,
    children: [{
      path: '/cesium',
      name: 'cesium',
      meta: {
        title: '铯地图'
      },
      component: Cesium
    }, {
      path: '/tencent',
      name: 'tencent',
      meta: {
        title: '腾讯地图'
      },
      component: Tencent
    }, {
      path: '/openlayer',
      name: 'openlayer',
      meta: {
        title: 'openlayer'
      },
      component: Openlayer
    },
    {
      path: '/starMap',
      name: 'starMap',
      meta: {
        title: '星点地图'
      },
      component: StarMap
    }]
  }, {
    path: '/layout',
    component: Layout,
    children: [{
      path: '/flex',
      name: 'flex',
      meta: {
        title: 'flex布局'
      },
      component: Flex
    }]
  }, {
    path: '/layout',
    component: Layout,
    children: [{
      path: '/cup',
      name: 'cup',
      meta: {
        title: '圣杯布局'
      },
      component: Cup
    }]
  }, {
    path: '/layout',
    component: Layout,
    children: [{
      path: '/center',
      name: 'center',
      meta: {
        title: '个人中心'
      },
      component: Center
    }]
  }, {
    path: '/layout',
    component: Layout,
    children: [
      {
        path: '/css',
        meta: {
          title: '特效案例'
        },
        component: Css
      }, {
        path: '/sidebar',
        name: 'sidebar',
        component: SideBar,
        meta: {
          title: '隐藏侧边栏'
        }
      }, {
        path: '/tableCompare',
        name: 'tableCompare',
        component: TableCompare,
        meta: {
          title: '表格对比'
        }
      }, {
        path: '/components',
        meta: {
          title: '组件汇总'
        },
        component: ComponentsDemo
      }, {
        path: '/test',
        meta: {
          title: '测试案例'
        },
        component: Test
      }
    ]
  }
]
const router = new Router({
  routes: routers
});

//路由守卫
router.beforeEach((to, from, next) => {
  // to要跳转到的路径
  // from从哪个路径来
  // next往下执行的回调
  // 在localStorage中获取token
  let token = localStorage.getItem('Authorization');
  // 判断该页面是否需要登录
  if (to.meta.auth) {
    // 如果token存在直接跳转
    if (token) {
      next()
    } else {
      // 否则跳转到login登录页面
      next({
        path: '/',
        // 跳转时传递参数到登录页面，以便登录后可以跳转到对应页面
        query: {
          redirect: to.fullPath
        }
      })
    }
  } else {
    // 如果不需要登录，则直接跳转到对应页面
    next()
  }
});
export default router;